package com.lovo.psc.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lovo.commom.PscRemote.IPscVendorRemote;
import com.lovo.commom.util.ImageysUtils;
import com.lovo.commom.util.MD5Encryptor;
import com.lovo.psc.bean.VerdorBean;
import com.lovo.psc.mapper.IVerdorMapper;
import com.lovo.psc.service.IVerdorService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.NoSuchAlgorithmException;

@Service
@Transactional
public class VerdorServiceImpl implements IVerdorService {
    @Autowired
    private IVerdorMapper verdorMapper;

    @Autowired
    RabbitTemplate rabbitTemplate;
    @Override
    public VerdorBean findByVerdoeName(String verdorName) {
        QueryWrapper<VerdorBean> qw = new QueryWrapper<>();
        qw.eq("v_name", verdorName);
        return verdorMapper.selectOne(qw);
    }

    @Override
    public void fastRegister(VerdorBean verdorBean) {
        verdorMapper.insert(verdorBean);
    }

    @Override
    public VerdorBean login(String name, String pwd) {
        QueryWrapper<VerdorBean> qw = new QueryWrapper<>();
        qw.eq("v_name", name);
        qw.eq("v_pwd", pwd);
        VerdorBean bean = verdorMapper.selectOne(qw);
        return bean;
    }

    @Override
    public void updateStatus(String vendorName, int status, String cause) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("v_name", vendorName);
        VerdorBean verdorBean = verdorMapper.selectOne(qw);
        verdorBean.setStatus(status);
        verdorBean.setCause(cause);
        verdorMapper.updateById(verdorBean);
    }

    @Override
    public VerdorBean findByName(String name) {
        QueryWrapper<VerdorBean> qw = new QueryWrapper<>();
        qw.eq("v_name",name);
        return verdorMapper.selectOne(qw);
    }

    @Override
    public Boolean replenishUserInfo(VerdorBean verdorBean) throws NoSuchAlgorithmException {
        QueryWrapper<VerdorBean> qw = new QueryWrapper<>();
        qw.eq("v_name", verdorBean.getName());
        VerdorBean verdorBean1 = verdorMapper.selectOne(qw);
        if (verdorBean1==null){
            verdorBean.setPwd(MD5Encryptor.encrypt(verdorBean.getPwd()));
            verdorBean.setStatus(0);
            //调用工具包压缩图片
            verdorBean.setAptitudeImage(ImageysUtils.resizeImageTo500K(verdorBean.getAptitudeImage()));
            verdorBean.setAptitudeImage(ImageysUtils.resizeImageTo500K(verdorBean.getIdImage()));
            String json = JSON.toJSONString(verdorBean);
            rabbitTemplate.convertAndSend("directExchangeHjh", "Hjh.key", json);
            verdorMapper.insert(verdorBean);
            return true;
        }else {
            return false;
        }

    }

    @Override
    public void again(VerdorBean verdorBean) {
        QueryWrapper<VerdorBean> qw = new QueryWrapper<>();
        qw.eq("v_name", verdorBean.getName());
        VerdorBean verdorBean1 = verdorMapper.selectOne(qw);
        //调用工具包压缩图片

        verdorBean1.setStatus(0);
        verdorBean1.setAddress(verdorBean.getAddress());
        verdorBean1.setBank(verdorBean.getBank());
        verdorBean1.setAptitudeImage(ImageysUtils.resizeImageTo500K(verdorBean.getAptitudeImage()));
        verdorBean1.setIdImage(ImageysUtils.resizeImageTo500K(verdorBean.getIdImage()));
        verdorBean1.setLeaderName(verdorBean.getLeaderName());
        verdorBean1.setLeaderPhone(verdorBean.getLeaderPhone());
        verdorBean1.setFirm(verdorBean.getFirm());
        verdorBean1.setRanges(verdorBean.getRanges());
        verdorMapper.updateById(verdorBean1);
        String json = JSON.toJSONString(verdorBean1);
        rabbitTemplate.convertAndSend("directExchangeHjh", "Hjh.key", json);
    }

}
